package cn.kgc.tangcco.service.impl;

import cn.hutool.setting.dialect.Props;
import cn.kgc.tangcco.mapper.StudentMapper;
import cn.kgc.tangcco.model.PageParam;
import cn.kgc.tangcco.model.ResponseText;
import cn.kgc.tangcco.model.ResultCode;
import cn.kgc.tangcco.pojo.Clazz;
import cn.kgc.tangcco.pojo.Student;
import cn.kgc.tangcco.service.StudentService;
import cn.kgc.tangcco.utils.cryptography.BaseCryptographyUtils;
import cn.kgc.tangcco.vo.StudentVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/10/30 11:29
 */
@Service
public class StudentServiceImpl implements StudentService {
    private static Props props = new Props("key.properties");
    private static String deskey = props.getProperty("deskey").trim();
    @Autowired
    private StudentMapper studentMapper;


    @Override
    public ResponseText add(List<Student> students) {
        if (students == null || students.size() == 0) {
            return new ResponseText(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
        }

        ResponseText ResponseText = new ResponseText(ResultCode.DATA_ERROR.getCode(), ResultCode.DATA_ERROR.getMsg(), null);
        int i = studentMapper.insertStudent(students);
        if (i > 0) {
            ResponseText.setCode(ResultCode.SUCCESS.getCode());
            ResponseText.setMsg(ResultCode.SUCCESS.getMsg());
        }
        return ResponseText;
    }

    @Override
    public ResponseText<List<Student>> query(Map<String, Object> paramMap) {
        ResponseText<List<Student>> ResponseText = new ResponseText<>(ResultCode.DATA_EMPTY.getCode(), ResultCode.DATA_ERROR.getMsg(), new ArrayList<Student>());
        // 从查询参数中获取分页参数对象
        PageParam pageParam = (PageParam) paramMap.get("pageParam");
        // 传入查询页码和每页记录数 注意代码顺序
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), "s.id desc");
        List<Student> students = studentMapper.selectStudent((Student) paramMap.get("student"));
        if (students != null || students.size() > 0) {
            ResponseText.setCode(ResultCode.SUCCESS.getCode());
            ResponseText.setCode(ResultCode.SUCCESS.getMsg());
            ResponseText.setData(students);
        }
        return ResponseText;
    }

    @Override
    public List<StudentVo> queryCountGroupByGender(Clazz clazz) {
        return studentMapper.selectCountGroupByGender(clazz);
    }

    @Override
    public ResponseText addStudent(Student student) {
        List<Student> students = new ArrayList<>();
        students.add(student);
        return add(students);
    }

    @Override
    public ResponseText queryMobileExists(String mobile) {
        if (studentMapper.selectMobile((mobile)) > 0) {
            return new ResponseText(ResultCode.MOBILE_EXISTS.getCode(), ResultCode.MOBILE_EXISTS.getMsg(), null);
        }
        return new ResponseText(ResultCode.MOBILE_NOT_EXISTS.getCode(), ResultCode.MOBILE_NOT_EXISTS.getMsg(), null);
    }

    @Override
    public ResponseText delelteStudent(List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return new ResponseText(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
        }
        if (studentMapper.delelteStudent(ids) > 0) {
            return new ResponseText(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
        }
        return new ResponseText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
    }

    @Override
    public ResponseText<Student> getStudentById(Integer id) {
        if (id == null || id == 0) {
            return new ResponseText<>(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
        }
        Student student = studentMapper.selectStudentById(id);
        if (student != null) {
            student.setAuthText(BaseCryptographyUtils.desDecodeHexString(student.getAuthText(), deskey));
            return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), student);
        }
        return new ResponseText(ResultCode.DATA_EMPTY.getCode(), ResultCode.DATA_EMPTY.getMsg(), null);
    }

    @Override
    public ResponseText updateStudent(Student student) {
        if (student == null || student.getId() == null || student.getAuthText() == null) {
            return new ResponseText(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
        }
        student.setAuthText(BaseCryptographyUtils.desEncodeHexString(student.getAuthText(), deskey));
        if (studentMapper.updateStudent(student) > 0) {
            return new ResponseText(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
        }
        return new ResponseText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
    }
}
